﻿//-----------------------------------------------------------------------
// <copyright file="ConsoleWriter.cs">
//     Copyright (c) Nicole Calinoiu. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;

namespace Bordecal.FxCop.Sdk.Testing.AppDomainCustomization
{
	[Serializable]
	[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented",
			Justification = "False positive.")]
	internal sealed class ConsoleWriter : AppDomainCustomizer<TextWriter>, IDisposable
	{
		private readonly StringWriter _writer = new StringWriter(CultureInfo.InvariantCulture);

		internal ConsoleWriter()
		{
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		private void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this._writer != null)
				{
					this._writer.Dispose();
				}
			}
		}

		protected override TextWriter CreateDataValue()
		{
			return this._writer;
		}

		protected override void DoCustomizeAppDomain(AppDomain targetDomain, TextWriter data)
		{
			Console.SetError(data);
			Console.SetOut(data);
		}

		public override string ToString()
		{
			return this._writer.ToString();
		}
	}
}